<?PHP
/**
 * index页面的功能处理类,处理由index页面内需要的功能,返回处理结果
 * 
 * @copyright Copyright (c) 2008 sdmblog (http://www.sdmblog.cn)
 * @author sdomain(sdomain@live.cn)
 * @version index.class.php Sat May 17 17:05:12 CST 2008 php $
 */
include_once('../model/index.model.php');
include_once ('../xajax/inc/xajax.inc.php');
$xajax = new xajax('index.class.php');
//显示文章列表
$xajax->registerFunction('getArticleList');
//显示公告
$xajax->registerFunction('getAnnounce');
//显示分类列表
$xajax->registerFunction('getCategoryList');
//日历
$xajax->registerFunction('getCalendar');
//显示最新文章
$xajax->registerFunction('getRecentArticles');
//显示回复
$xajax->registerFunction('getComment');
//网站统计
$xajax->registerFunction('getStats');
//友情连接
$xajax->registerFunction('getLinks');
//用户登录
$xajax->registerFunction('userLogin');

include_once('../include/ajax.common.php');
/**
 * 取得文章列表
 *
 * @return ajaxObj
 */
function getArticleList($page=1,$condition='',$content='')
{
	$obj = new xajaxResponse();
	try {
		
		
		global $lang;
		global $webstate;
		global $vmodel;
		if ($webstate == 0) {
			$obj->alert($lang[WEB_ERROR_0001]);
			return $obj;
		}
		$category=isset($category)?$category:0;
		$page = isset($page)?$page:0;
		
		if (!is_numeric($page)) {
			$obj->alert('参数传递错误');
			return $obj;
		}
		
		$index = new model_index();
		$articles = $index->getArticleList($page,$condition,$content);
		//公告......................................
		
		if ($index->queryCount == 0) {
			//查到内容为空
			$pageinfo=pageinfo('index.php',$page,$index->pageCount,$condition,$content,1,true,'getArticleList');
			$obj->assign('pages','innerHTML',$pageinfo);
			$obj->assign('no-entry-message','innerHTML',$lang['NO_ANY_ARTICLES']);
		}else {
			//否则显示分页和文章信息
			//分页.....................................
			
			$pageinfo=pageinfo('index.php',$page,$index->pageCount,$condition,$content,1,true,'getArticleList');
			//$obj->alert($pageinfo);
			$obj->assign('pages','innerHTML',$pageinfo);
			//文章..........................................
			
			$strHTML = $index->getArticleHtml($articles,$vmodel);
			$obj->assign('no-entry-message','style.display','none');
			$obj->assign('textbox','style.display','');
			$obj->assign('textbox','innerHTML',$strHTML);
			//$obj->alert($strHTML);
			
		}
		
	}
	catch (Exception $e){
		//$obj->assign('mainWrapper','innerHTML',$index->sql.$e->getMessage());
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 获取公告--当前公告只有一条，日后可能会增加多条公告，也可叫作心情等
 *
 * @return ajaxObj
 */
function getAnnounce()
{
	$obj = new xajaxResponse();
	global $lang;
	$announce = GP('announce');
	$announce = $announce?$announce:$lang['NOANNOUNCE'];
	$obj->assign('announce-content','innerHTML',$announce);
	return $obj;
}
/**
 * 获取分类列表
 *
 * @return ajaxObj
 */
function getCategoryList()
{
	$obj = new xajaxResponse();
	try {
		$index = new model_index();
		$categories = $index->getCategoryList();
		$strHTML = $index->getCategoryHtml($categories);
		$obj->assign('panelCategoryContent','innerHTML',$strHTML);
	}catch (Exception $e){
		//$obj->assign('mainWrapper','innerHTML',$index->sql.$e->getMessage());
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 获取最近发表的文章（10个）
 *
 * @return ajaxObj
 */
function getRecentArticles()
{
	$obj = new xajaxResponse();
	try {
		$index = new model_index();
		$recentArticle = $index->getRecentArticles();
		$strHTML = $index->getRecentHtml($recentArticle);
		$obj->assign('panelArticleContent','innerHTML',$strHTML);
		//$obj->alert($strHTML);
	}
	catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 文章回复列表-10个最新
 * 
 * @return ajaxObj
 */
function getComment()
{
	$obj = new xajaxResponse();
	try {
		$index = new model_index();
		$comment = $index->getComment();
		$strHTML = $index->getCommentHtml($comment);
		$obj->assign('panelCommentContent','innerHTML',$strHTML);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 获取日历
 * @param int $year 年份
 * @param int $month 月份
 * @return ajaxObj
 */
function getCalendar($year,$month)
{
	$obj = new xajaxResponse();
	try {
		if (!is_numeric($year)||!is_numeric($month)) {
			$obj->alert('无效日期');
			return $obj;
		}
		if ($year>3000 || $year < 1900) {
			$obj->alert('无效日期');
			return $obj;
		}
		if ($month<1 || $month>12) {
			$obj->alert('无效日期');
			return $obj;
		}
		$index = new model_index();
		$articleList = $index->getMonthArticleList($year,$month);
		$strHTML = $index->getCalendarHtml($year,$month,$articleList);
		//$obj->assign('panelCalendar','innerHTML','<h5>日历</h5><li><a href="#" onclick="getArticleListEx(0,0,\'date\',\'2008-4-16\');">无数据库</a></li>');
		$obj->assign('panelCalendar','innerHTML',$strHTML);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 网站统计及状态数据
 *
 * @return ajaxObj
 */
function getStats()
{
	$obj = new xajaxResponse();
	try {
		$index = new model_index();
		$stats = $index->getStatsData();
		$strHTML = $index->getStatsHtml($stats);
		$obj->assign('panelStatsContent','innerHTML',$strHTML);
		//$obj->alert('sdf');
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 友情连接
 *
 * @return ajaxObj
 */
function getLinks()
{
	$obj = new xajaxResponse();
	try {
		$obj->assign('panelLinksContent','innerHTML','友情连接');
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;	
}
function userLogin($loginInfo)
{
	$obj = new xajaxResponse();
	try{
		session_start();
		$username = $loginInfo['username'];
		$authnum = $loginInfo['authnum'];
		if($authnum!=$_SESSION['auth_num']){
			$obj->alert('请输入正确的验证码');
			$obj->assign('Loginbtn','disabled',false);
			return $obj;
		}
		if(!isUserName($username)){
			$obj->alert('非法的用户名，请输入合法的用户名');
			$obj->assign('Loginbtn','disabled',false);
			return $obj;
		}
		include_once('../model/user.model.php');
		$user = new model_user;
		$res = $user->login($username,$loginInfo['password']);
		if($res!=1){
			$obj->assign('Loginbtn','disabled',false);
			$obj->alert('用户名或密码错误，请重新登录');
			refreshAuthnum('authcode',$obj);
			return $obj;
		}
		$strHTML = $user->getUserHTML($user->userInfo);
		//$userHTML = $_COOKIE
		$obj->assign('panel_user','innerHTML',$strHTML);
		$obj->assign('panel_user','style.display','block');
		$obj->assign('panelUser','style.display','none');
		$obj->assign('dologin','style.display','none');
			
	}catch(Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
unset($GLOBALS);
$xajax->processRequest();
?>